package br.com.simora.email;

import java.util.Date;

import javax.inject.Named;
import javax.persistence.EntityManager;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;

import br.com.simora.persistence.AbstractDAO;

@Named("emailDAO")
public class EmailDAO extends AbstractDAO {
	//private TbMailConfig mailConfig;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7257051605064346853L;

	public EmailDAO(EntityManager entityManager) {
		this.manager = entityManager;
	}
	
	public void enviaEmailEmployeeIncluded(Date dataInicio, Date dataFim, Integer idWorkLocation) {
		String msg = "";
		/*Email email = criaEmail("myteamatwork@innersmart.com.br", employee.getTbCompany().getNmEmail(), msg);
		email.setSubject("MyTeamAtWork - Funcionário escalado");
		enviaEmail(email);*/
	}
	
	private Boolean enviaEmail(Email email) {
		try {
			email.send();
			return true;
		} catch (EmailException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/*private TbMailConfig getMailConfig(){
		if(mailConfig == null){
			mailConfig = manager.find(TbMailConfig.class, 1);
		}
		return mailConfig;
	}*/
	
	private Email criaEmail(String emailFrom, String emailTo, String msg){
		/*TbMailConfig mail = getMailConfig();
		HtmlEmail email = new HtmlEmail();
		email.setHostName(mail.getHostName());
		email.setSmtpPort(mail.getSmtpPort());
		email.setAuthenticator(new DefaultAuthenticator(mail.getNmUser(), mail.getNmPassword()));
		email.setSSLOnConnect(true);
		try {
			email.getMailSession().getProperties().put("mail.smtps.auth", "true");
			email.getMailSession().getProperties().put("mail.debug", "true");
			email.getMailSession().getProperties().put("mail.smtps.port", mail.getSmtpPort());
			email.getMailSession().getProperties().put("mail.smtps.socketFactory.port", mail.getSmtpPort());
			email.getMailSession().getProperties().put("mail.smtps.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
			email.getMailSession().getProperties().put("mail.smtps.socketFactory.fallback", "false");
			email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
			if(!StringUteis.isEmpty(emailFrom)){
				email.setFrom(emailFrom);
			} else {
				email.setFrom(mail.getNmMailAdmin());
			}
			email.setHtmlMsg(msg);
			email.addTo(emailTo);
		} catch (EmailException e) {
			e.printStackTrace();
		}
		return email;*/
		return null;
	}
	
}